package pl.gettoknowme.mobile.android.network.request.impl;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;

import flexjson.JSONSerializer;

import pl.gettoknowme.mobile.android.network.request.HttpRequestWrapper;
import pl.gettoknowme.mobile.android.util.Constants;
import pl.gettoknowme.mobile.android.util.LoggingUtil;

/**
 * Wrapper żądania dostarczającego predykcję płci na podstawie treści SMS-ów
 * @author Michał Kołodziejski
 */
public class SmsSexAnalysisHttpRequestWrapper implements HttpRequestWrapper {

    /**
     * Dane analizy
     */
    public class SmsSexAnalysisDataObject {
        protected boolean isAnalysisSuccessful;
        protected float maleProbability;
        
        public boolean getIsAnalysisSuccessful() {
            return isAnalysisSuccessful;
        }
        
        public void setIsAnalysisSuccessful(boolean isAnalysisSuccessful) {
            this.isAnalysisSuccessful = isAnalysisSuccessful;
        }
        
        public float getMaleProbability() {
            return maleProbability;
        }
        
        public void setMaleProbability(float maleProbability) {
            this.maleProbability = maleProbability;
        }
    }
    
    /**
     * Identyfikator użytkownika
     */
    private String userId;
    /**
     * Czy analiza się powiodła
     */
    private boolean isAnalysisSuccessful;
    /**
     * Prawdopodobieństwo, że uzytkownik jest mężczyzną
     */
    private float maleProbability;
    
    /**
     * Tworzy wrapper żądania dostarczającego listę zebranych statystyk SMS-ów
     * @param locationList lista zebranych statystyk SMS-ów do przesłania na serwer
     */
    public SmsSexAnalysisHttpRequestWrapper(String userId, boolean isAnalysisSuccessful,
            float maleProbability) {
        this.userId = userId;
        this.isAnalysisSuccessful = isAnalysisSuccessful;
        this.maleProbability = maleProbability;
    }
    
    
    @Override
    public HttpUriRequest getRequest() {
        String uri = Constants.SERVER_HOST + "/" + Constants.API_APP_ID + "/sms_sex_analysis";
        HttpPost httpPost = new HttpPost(uri);
        httpPost.setEntity(HttpRequestUtil.createHttpEntity(serializeData()));
        return httpPost;
    }

    protected String serializeData(){
        SmsSexAnalysisDataObject dataObject = new SmsSexAnalysisDataObject();
        dataObject.setIsAnalysisSuccessful(isAnalysisSuccessful);
        dataObject.setMaleProbability(maleProbability);
        APIData apiData = new APIData(userId, dataObject);
        
        JSONSerializer serializer = new JSONSerializer();
        String serialized = serializer
                .exclude("*.class")
                .deepSerialize(apiData);
        LoggingUtil.d(this, "Serialized data: "+serialized);
        return serialized;
    }
}
